/* 附身修改 */
<template>
  <div>
    <Card :bordered="false">
      <p slot="title">复审修改页</p>
      <table style="margin-bottom:20px" cellspacing="0" cellpadding="0" border="1" width="100%" ref="print">
        <tr>
          <th colspan="6">
            复审修改
          </th>
        </tr>
        <tr>
          <td width="25%">
            修改人员
          </td>
          <td width="25%" class="sizeStyle">
            {{info.RealName}}
          </td>
          <td width="25%">
            修改完成时间
          </td>
          <td class="sizeStyle">
            {{info.ModifierDotime}}
          </td>
        </tr>
      </table>
      <table style="margin-bottom:20px" cellspacing="0" cellpadding="0" border="0" width="100%">
        <tr>
          <th>
            复审修改清单
          </th>
        </tr>
        <tr>
          <td>
            <div style="width:99%;margin:auto;height:100%;">
              <Table border ref="selection" :columns="columns1" :data="info.data"></Table>
              <Row>
                <Col span="24" style="text-align: center;padding:10px">
                <Button type="primary" @click="passFS" :disabled="Zbol">通过复审</Button>
                </Col>
              </Row>
            </div>
          </td>
        </tr>
      </table>
      <table style="margin-bottom:20px" cellspacing="0" cellpadding="0" border="1" width="100%">
        <tr>
          <th colspan="4">
            复审修改清单2
          </th>
        </tr>
        <tr>
          <td width="25%" class="ivu-table-cell">复审工程师</td>
          <td width="25%" class="sizeStyle">
            {{info.Audit}}
          </td>
          <td width="25%">复审本次意见时间</td>
          <td width="25%" class="sizeStyle">
            {{this.$moment().format('YYYY-MM-DD')}}
          </td>
        </tr>
        <tr>
          <td colspan="4">
            <div style="width:99%;margin:auto;height:100%;">
              <Table border ref="selection" :columns="FushenColumns" :data="FushenXg" @on-row-click="getIndex"></Table>
              <Row>
                <Col span="24" style="text-align: center;padding:10px">
                <Button type="primary" @click="inputAdd('FushenXg')" :disabled="Zbol">添加</Button>
                <Button type="error" @click="inputRemove('FushenXg')" :disabled="Zbol">删除</Button>
                </Col>
              </Row>
            </div>
          </td>
        </tr>
        <tr>
          <td>
            报告（正文、附图、附件、附表）
          </td>

          <td colspan="3">
            <Button :disabled='!ProjectEnvOverview?true:false'  :to="ProjectEnvOverview" target='_blank' style="float:left;margin-left:50px">{{xiazai}}</Button>
            <div>
              <Upload with-credentials :before-upload="upLoadFun" :data='imgUpload' :action="shishi" ref="uploadButton">
                <Button :disabled="Zbol">上传</Button>
              </Upload>
            </div>
          </td>

        </tr>
      </table>

      <Row style="padding:40px 0">
        <Col span="12" style="text-align: left">
        <!-- <Button type="primary" @click="$print($refs.print)">打印</Button> -->
        <PrintButton Url="print/printAuditForm" :Project_id="info.Id || $route.query.ProjectId" Atype="复审"></PrintButton>
        </Col>
        <Col span="12" style="text-align: right;">
        <Button type="primary" @click="submit('')" :disabled="Zbol">保存</Button>
        <Button type="primary" @click="submit(2)" :disabled="Zbol">发送意见</Button>
        </Col>
      </Row>

    </Card>
  </div>
</template>

<script>
  import libs from "../../../libs/uploads";
  export default {
    data() {
      return {
        Zbol :false,
        shishi:'',
        imgUpload: {
          Id: this.$route.query.ProjectId,
          Key: 'ImgUrl'
        },
        info: {},
        setTimeData: 0,
        columns1: [{
            align: "center",
            type: "selection",
            width: 60,
            align: "center"
          },
          {
            align: "center",
            width: 80,
            title: "序号",
            type: 'index'
          },
          {
            align: "center",
            title: "复审意见",
            key: "AuditOpinion",
          },
          {
            align: "center",
            title: "修改说明",
            key: "ReviseExplain"
          },
        ],
        FushenColumns: [
          {
            align: 'center',
            title: '序号',
            type: 'index',
            width: 80,
          },
          {
            align: 'center',
            title: '复审意见',
            key: 'AuditOpinion',
            render: (h, {
              row,
              index
            }) => {
              return h('Input', {
                props: {
                  type: 'textarea',
                  autosize: true,
                  value: row.AuditOpinion
                },
                on: {
                  'on-blur': (val) => {
                    this.FushenXg[index].AuditOpinion = val.target.value
                    console.log('失去焦点查看：' + this.FushenXg[index].AuditOpinion)
                  }
                }
              })
            }
          },
          // {
          //   align: 'center',
          //   title: '修改说明',
          //   key: 'Revise_explain',
          // }
        ],
        FushenXg: [],
        ProjectEnvOverview: '',
        xiazai:'下载',
        proType: 0,
        outTime: 0,
        fsPass: 0,
      };
    },
    mounted() {
      console.log('复审')
      let query = this.$route.query
      this.query = query
      this.findByProject(query.ProjectId, query.Atype)
      let delinfo = ''
      this.shishi = libs.uploads();
    },
    methods: {
      inputAdd(type) {
        this.FushenXg.push({AuditTime:new Date().getTime()})
      },
      inputRemove(type) {
        // console.log(type)
        // console.log(this[type])
        // console.log(this.allIndex)
        // 保存回显，点删除要删除数据库里面，新删除不需要
        var delId = this[type][this.allIndex].Id
        if(this.delinfo == '审核中'){
          if(delId) {
            axios.post('/auditreply/deleteByid',{Id:delId}).then(res=>{
              if(res._STATUS==1) {
                this.$Message.success('删除成功！')
                this[type].splice(this.allIndex, 1); //删除数据库后本地也删除
              }else {
                this.$Message.error(res._MESSAGE)
              }
            })
          }else {
            this[type].splice(this.allIndex, 1);
            console.log(this[type] + ' 被删除了')
          } 
        } else if (this.delinfo == '未提交'){
          alert('审核未提交，无法进行修改')
          return
        } else if (this.delinfo == '已审核'){
          alert('审核已结束，无法进行修改')
          return
        }
      },
      getIndex(data, index, event) {
        this.allIndex = index;
      },
      findByProject(ProjectId, Atype) {
        // axios.get('/audit/findByProject2', {
        this.$post('audit.findByProject2', {
            Atype,
            ProjectId
        }).then(res => {
          // 把点保存时 填写数据过滤出来,初审修改清单中不会显示
          if(res.data&&res.data.length>0) {
            res.data = res.data.filter((ele)=>{
              if(ele.ChangeTime==null&&ele.ReviseExplain==null) {
                this.FushenXg.push(ele)
              }
              return ele.ChangeTime!=null&&ele.ReviseExplain!=null
            })
          }
          this.getDownInfo()
          this.info = res
          this.delinfo = res.Stats
          console.log(res)
          var number = this.info.AuditTime.length + 1
          this.imgUpload.Name = '复审' + number + '批'
          this.chushenLoading = false
          if(res.Stats == '审核中'){
            this.proType = 1
          }
          if(res.Stats == '未提交'){
            this.Zbol = true;
          }
        })
      },
      getDownInfo(){
        axios.post("wechatpublic/findImg",{
          Id:this.$route.query.ProjectId
        }).then((res)=>{
          if(this.info.Stats == '未提交'){
            this.ProjectEnvOverview =''
          }else{
            for(var i=0;i<res.data.length;i++){
              if(res.data[i].Status == '复审'){
                this.ProjectEnvOverview = res.data[i].ImgUrl
                this.xiazai = decodeURIComponent(this.ProjectEnvOverview.split('/')[5].substring(5,this.ProjectEnvOverview.split('/')[5].lastIndexOf('?')))
              }
            }
          }
        })
      },
      upLoadFun(){
         this.$refs.uploadButton.clearFiles()
        if(this.proType != 1){
          this.$Message.warning('审核未开始，无法上传文件')
          return false
        }
      },
      submit(type) {
        if(this.outTime == 0){
          this.outTime = 1
          var bl = false;
          setTimeout(()=>{this.outTime = 0},5000)
          let query = this.query
          let AuditReplyLists = this.FushenXg
          AuditReplyLists.map(i=>{
            if(i.AuditOpinion && i.AuditOpinion != ''){
              
            }else{
              bl = true
            }
          })
          if(bl){
            this.outTime = 0
            alert('请填写完整意见后，再发送意见！')
            return
          }

          var dataArr = Object.keys(AuditReplyLists).length <=0 ? [] : '123'
          for (let val of AuditReplyLists) {
            val['AuditId'] = query.Id
            // val['Audit_time'] = this.$moment().format('YYYY-MM-DD HH:mm:ss')
          }
          if (Object.keys(AuditReplyLists).length <= 0) {
            this.$Modal.error({
              title: '请输入意见'
            })
          } else {
            if(type == ''){
              if(this.setTimeData == 0){
                this.setTimeData = 1
                setTimeout(()=>{this.setTimeData = 0},5000)
                axios.get('/audit/isAuditb', {
                  params: {
                    ProjectId: query.ProjectId,
                    Btype: query.Atype,
                    OK: 1,
                    Save: type,
                    AuditReplyLists: dataArr,
                  }
                }).then(res => {
                  let title = res._MESSAGE
                  if (res._STATUS == 1) {
                    this.$Modal.success({
                      title,
                    })
                    axios.post('/auditreply/saveOrUpdateM', {
                      AuditReplyLists:JSON.stringify(AuditReplyLists),
                      ProjectId: this.$route.query.ProjectId,
                      Atype: this.$route.query.Atype,
                      Aoc: '2',
                      Save: type
                    }).then(res => {
                      this.$router.push({name: 'myProjectCheck'})
                    }).catch(err => {
                      this.$Modal.error({
                        title: '发送失败'
                      })
                    })
                  } else {
                    this.$Modal.error({
                      title,
                    })
                  }
                })
              }else{
                this.$Message.warning('请勿重复保存！')
                return
              }
            }else {
              axios.get('/audit/isAuditb', {
                params: {
                  ProjectId: query.ProjectId,
                  Btype: query.Atype,
                  OK: 1,
                  Save: type,
                  AuditReplyLists: dataArr,
                }
              }).then(res => {
                let title = res._MESSAGE
                if (res._STATUS == 1) {
                  if(type == 2){
                    social({
                      method: 'post',
                      url: '/info/notice',
                      params: {
                        content: this.info.ProjectName +'项目复审工程师提出审核意见，请尽快进行修改！',
                        app_name: '环评管控系统',
                        app: 'eao',
                        type: this.info.StaffNamesId + '',
                        uid: -1
                      },
                      }).then((res)=>{
                        console.log(res)
                      }).catch((err)=>{
                        console.log(err)
                      })
                  }
                  this.$Modal.success({
                    title,
                  })
                  axios.post('/auditreply/saveOrUpdateM', {
                    AuditReplyLists:JSON.stringify(AuditReplyLists),
                    ProjectId: this.$route.query.ProjectId,
                    Atype: this.$route.query.Atype,
                    Aoc: '2',
                    Save: type
                  }).then(res => {
                    // 保存、发送意见成功后跳转到正在审核页面
                    this.$router.push({name: 'myProjectCheck'})
                  }).catch(err => {
                    this.$Modal.error({
                      title: '发送失败'
                    })
                  })
                } else {
                  this.$Modal.error({
                    title,
                  })
                }
              })
            }
          }
        }else{
          console.log('别点了老铁')
        }
      },
      passFS() {
        if(this.fsPass == 0){
          this.fsPass = 1
          setTimeout(()=>{this.fsPass = 0},5000)
          axios.get('/audit/isAuditb', {
            params: {
              ProjectId: this.$route.query.ProjectId,
              Btype: this.$route.query.Atype,
              OK: 0,
              AuditReplyLists: '123'
            }
          }).then(res => {
            let title = res._MESSAGE
            if (res._STATUS == 1) {
              social({
                method: 'post',
                url: '/info/notice',
                params: {
                  content: this.info.ProjectName +'项目已通过复审工程师审核，已经递交到终审！',
                  app_name: '环评管控系统',
                  app: 'eao',
                  type: this.info.StaffNamesId + '',
                  uid: -1
                },
                }).then((res)=>{
                  console.log(res)
                }).catch((err)=>{
                  console.log(err)
              })
              social({
                method: 'post',
                url: '/info/notice',
                params: {
                  content: this.info.ProjectName +'项目已通过你的审核，已经递交到终审！',
                  app_name: '环评管控系统',
                  app: 'eao',
                  type: this.info.auditor + '',
                  uid: -1
                },
                }).then((res)=>{
                  console.log(res)
                }).catch((err)=>{
                  console.log(err)
              })
              this.$Modal.success({
                title:title,
                onOk:()=>{
                  this.$router.push({name: 'myProjectCheck',params: {success:1}})
                }
              })
            } else {
              this.$Modal.error({
                title
              })
            }
          })
        }
      },
    }
  };
</script>

<style lang="less" scoped>
  /deep/ textarea.ivu-input {
    font-size: 12px;
  }
  .sizeStyle {
    font-size:12px;
  }
</style>